﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;

namespace AgvPublic
{
   public delegate void AgvInfoDelegate(string ip,byte[] buffer);

        public class UdpManager
        {
            public event AgvInfoDelegate AgvInfoEvent;
            // 定义节点
            private IPEndPoint ipEndPoint = null;
            private IPEndPoint ChargeEndPoint = null;

            // 定义AgvUDP发送和接收
            private UdpClient udpReceive = null;
            private UdpClient udpSend = null;

            //定义充电桩Udp发送和接收
            private UdpClient chargeUdpReceive = null;
            private UdpClient chargeUdpSend = null;

            // 定义Agv端口
            private const int listenPort = 8080;
            private const int remotePort = 8080;

            //定义充电桩端口
            private const int E_listenPort = 8081;
            private const int E_remotePort = 8080;

            //Agv接收发送状态类
            UdpStateModel udpReceiveState = null;
            UdpStateModel udpSendState = null;

            //充电桩接收发送状态类
            UdpStateModel chargeReceiveState = null;
            UdpStateModel chargeSendState = null;
      
            public UdpManager()
            {
                // 本机节点
                ipEndPoint = new IPEndPoint(IPAddress.Any, listenPort);
                // 远程节点

                // Agv实例化
                udpReceive = new UdpClient(ipEndPoint);
                udpReceiveState = new UdpStateModel();
                udpReceiveState.udpClient = udpReceive;
                udpReceiveState.ipEndPoint = ipEndPoint;

                udpSend = new UdpClient();
                udpSendState = new UdpStateModel();
                udpSendState.udpClient = udpSend;

            }

            /// <summary>
            /// AGV接收数据
            /// </summary>
            public void ReceiveMsg()
            {
                // 调用接收回调函数
                IAsyncResult iar = udpReceive.BeginReceive(new AsyncCallback(ReceiveCallback), udpReceiveState);
            }

            /// <summary>
            /// 接收回调函数
            /// </summary>
            /// <param name="iar"></param>
            private void ReceiveCallback(IAsyncResult iar)
            {
                try
                {
                    ReceiveMsg();
                    UdpStateModel udpReceiveState = iar.AsyncState as UdpStateModel;
                    if (iar.IsCompleted)
                    {
                        //获取小车发送报文
                        Byte[] receiveBytes = udpReceiveState.udpClient.EndReceive(iar, ref udpReceiveState.ipEndPoint);
                        string ip = udpReceiveState.ipEndPoint.ToString().Split(':')[0];
                        AgvInfoEvent(ip, receiveBytes);
                    }
                }

                catch (Exception ex)

                {

                }
            }

            public void SendMsg(string ip, byte[] msg)
            {
                udpSendState.ipEndPoint = new IPEndPoint(IPAddress.Parse(ip), remotePort);
                udpSend.Connect(udpSendState.ipEndPoint);
                udpSendState.udpClient = udpSend;
                udpSend.BeginSend(msg, msg.Length, new AsyncCallback(SendCallback), udpSendState);                
            }

            // 发送回调函数
            private void SendCallback(IAsyncResult iar)
            {
                UdpStateModel udpState = iar.AsyncState as UdpStateModel;
                udpState.udpClient.EndSend(iar);
            }

        }
    
}
